package com.fdd.furns.dao.impl;

import com.fdd.furns.dao.BasicDAO;
import com.fdd.furns.dao.FurnDAO;
import com.fdd.furns.pojo.Furn;

import java.util.Collections;
import java.util.List;

public class FurnDAOImpl extends BasicDAO implements FurnDAO{

    @Override
    public List<Furn> queryFurns() {
        String sql = "select `id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path` imgPath from furn";
        return queryMulti(sql, Furn.class);
    }

    @Override
    public int addFurn(Furn furn) {
        String sql = "insert into furn(`name`, `maker`, `price`, `sales`, `stock`, `img_path`) values(?, ?, ?, ?, ?, ?)";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
    }

    @Override
    public int deleteFurnById(int id) {
        String sql = "delete from furn where id = ?";
        return update(sql, id);
    }

    @Override
    public Furn queryFurnById(int id) {
        String sql = "select `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath from furn where id = ?";
        return (Furn)querySingle(sql, Furn.class, id);
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql = "update furn set `name`=?, `maker`=?, `price`=?, `sales`=?, `stock`=?, `img_path`=? where id =?";
        return update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(), furn.getId());
    }

    @Override
    public List<Furn> getPageItems(int pageNo, int pageSize) {
        String sql = "select `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath from furn limit ?,?";
        return queryMulti(sql, Furn.class, (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public int queryTotalRow() {
        String sql = "select count(*) from furn";
        return ((Number) queryScalar(sql)).intValue();
    }



    public List<Furn> getPageItemsByName(int pageNo, int pageSize, String name) {
        String sql = "select `id`, `name`, `maker`, `price`, `sales`, `stock`, `img_path` imgPath from furn where name like ? limit ?,? ";
        return queryMulti(sql, Furn.class, "%" + name + "%", (pageNo - 1) * pageSize, pageSize);
    }

    @Override
    public int queryTotalRowByName(String name) {
        String sql = "select count(*) from furn where name like?";
        return ((Number)queryScalar(sql, "%" + name + "%")).intValue();
    }
}
